import { Controller, Get, Post, Body, Param, Put, Delete, QueryParams, Req, Res } from 'routing-controllers';
import { Container } from 'typedi';
import { DictService } from '@services/dict.service';
import { CreateDictWithItemsDto, DictPageDto, UpdateDictWithItemsDto } from '@/dtos';
import { Log, LogOperation } from '@decorators/logs.decorators';
import { ApiResponseBuilder } from '@utils/api-response-builder';
import { Request, Response } from 'express';
import { OpenAPI } from 'routing-controllers-openapi';
import { RequiredPerm } from '@decorators/perm.decorator';

@Controller('/dicts')
export class DictController {
  private dict = Container.get(DictService);

  @Post('/create')
  @RequiredPerm('dicts:create')
  @OpenAPI({ summary: '创建字典' })
  @Log({
    bizType: 'dict',
    operation: LogOperation.CREATE,
    bizId: (req: { user: { id: string } }) => 'dict_create_' + (req?.user?.id ?? ''),
  })
  async create(@Req() req: Request, @Res() res: Response, @Body() dto: CreateDictWithItemsDto) {
    await this.dict.createWithItems(dto);
    return new ApiResponseBuilder(req, res).success();
  }

  @Put('/update/:code')
  @RequiredPerm('dicts:update')
  @OpenAPI({ summary: '更新字典' })
  @Log({
    bizType: 'dict',
    operation: LogOperation.UPDATE,
    bizId: (req: { user: { id: string } }) => 'dict_update_' + (req?.user?.id ?? ''),
  })
  async update(@Req() req: Request, @Res() res: Response, @Param('code') code: string, @Body() dto: UpdateDictWithItemsDto) {
    await this.dict.updateWithItems(code, dto);
    return new ApiResponseBuilder(req, res).success();
  }

  @Get('/page')
  @RequiredPerm('dicts:page')
  @OpenAPI({ summary: '获取字典分页列表' })
  async findAll(@QueryParams() pageDto: DictPageDto, @Req() req: Request, @Res() res: Response) {
    const { list, total } = await this.dict.findAllWithItems(pageDto);
    return new ApiResponseBuilder(req, res).paginate(list, total, pageDto.pageNum || 1, pageDto.pageSize || 20);
  }

  @Get('/list')
  @RequiredPerm('dicts:list')
  @OpenAPI({ summary: '获取字典键列表' })
  async getDictKeys(@Req() req: Request, @Res() res: Response) {
    const result = await this.dict.getDictKeys();
    return new ApiResponseBuilder(req, res).success(result);
  }

  @Get('/detail/:code')
  @RequiredPerm('dicts:detail')
  @OpenAPI({ summary: '获取字典中启动的所有字典项' })
  async findOne(@Param('code') code: string, @Req() req: Request, @Res() res: Response) {
    const dict = await this.dict.findOneWithItems(code);
    return new ApiResponseBuilder(req, res).success(dict);
  }

  @Get('/detail/all/:code')
  @RequiredPerm('dicts:detail:all')
  @OpenAPI({ summary: '获取字典中的所有字典项' })
  async getAllDictItem(@Param('code') code: string, @Req() req: Request, @Res() res: Response) {
    const dict = await this.dict.findOneWithItems(code, true);
    return new ApiResponseBuilder(req, res).success(dict);
  }

  @Delete('/delete/:code')
  @RequiredPerm('dicts:delete')
  @OpenAPI({ summary: '删除字典' })
  @Log({
    bizType: 'dict',
    operation: LogOperation.DELETE,
    bizId: (req: { user: { id: string } }) => 'dict_delete_' + (req?.user?.id ?? ''),
  })
  async remove(@Param('code') code: string, @Req() req: Request, @Res() res: Response) {
    await this.dict.remove(code);
    return new ApiResponseBuilder(req, res).success();
  }
}
